package cn.wk.servlet;

import cn.wk.entity.Course;
import cn.wk.services.CourseServices;
import cn.wk.services.UserServices;
import cn.wk.services.impl.CourseServicesImpl;
import cn.wk.services.impl.UserServicesImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/login")
public class ServletLogin extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 接收参数之前转换
        request.setCharacterEncoding("utf-8");
        //2.接收参数
        String roel = request.getParameter("roel");
        String no = request.getParameter("no");
        String pwd=request.getParameter("pwd");
        UserServices us=new UserServicesImpl();
       int b= us.login(roel,no,pwd);
       if(b>0){
           request.setAttribute("ok","登录成功！");
           HttpSession session = request.getSession();
           session.setAttribute("roel",roel);
           session.setAttribute("no",no);
           session.setMaxInactiveInterval(60*60*24);
           if(roel.equals("教师")){
               CourseServices cs=new CourseServicesImpl();
               List<Course> counameByTno = cs.getCounameByTno(no);
               request.setAttribute("listcourse",counameByTno);
           }

           request.getRequestDispatcher("/index1.jsp").forward(request,response);
       }else {
           request.setAttribute("ok","登录失败！");
           request.getRequestDispatcher("/login.jsp").forward(request,response);
       }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
